package com.ccl.service.impl;

import com.ccl.dao.ArticleMapper;
import com.ccl.dao.CommentMapper;
import com.ccl.dao.StatisticMapper;
import com.ccl.entity.Article;
import com.ccl.entity.Statistic;
import com.ccl.service.ArticleService;
import com.ccl.service.CommentService;
import com.ccl.service.StatisticService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CommentMapper commentMapper;

    //分页查询
    @Override
    public PageInfo<Article> findArticleWithPage(Integer page, Integer count) {
        PageHelper.startPage(page,count);
        List<Article> articles = articleMapper.findAll();
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    //根据id查询
    @Override
    public Article findById(Integer id) {
        //从缓存中查找文章内容
        Article article = (Article) redisTemplate.opsForValue().get("article_" + id);
        //判断
        if (article==null){
            //redis中未查询到,从数据库中查询
            article = articleMapper.findById(id);
            //保存一份到redis中
            redisTemplate.opsForValue().set("article_"+id,article);
        }
        return article;
    }

    //获取排行榜数据
    @Override
    public List<Article> getHeatData() {
        List<Article> heatData = articleMapper.getHeatData();
        return heatData;
     /*   //文章id/文章点击量/文章评论量
     *//*   List<Statistic> statistics = statisticMapper.findHeadData();
        List<Article> articleList = new ArrayList();
        //遍历
        for (Statistic statistic : statistics) {
            //获取文章点击量
            Integer hits = statistic.getHits();
            //获取文章评论量
            Integer commentsNum = statistic.getCommentsNum();
            //获取文章id
            Integer articleId = statistic.getArticleId();
            Article article = articleMapper.findById(articleId);
            //将点击量和评论封装到article中
            article.setHits(hits);
            article.setCommentsNum(commentsNum);

            //将内容、点击量、评论，保存到articleList集合中
            articleList.add(article);*/
        }


    //获取文章数量
    @Override
    public Integer getArticlesNum() {
        return articleMapper.getArticlesNum();
    }
    //获取后台最新文章
    @Override
    public List<Article> getRecentArticles(Integer num) {
        List<Article> recentArticles = articleMapper.getRecentArticles(num);
  /*      //遍历文章集合
        for (Article artilce : recentArticles) {
            //查询文章点击量
            Statistic statistic = statisticMapper.findByArticleId(artilce.getId());
            Integer hits = statistic.getHits();
            Integer commentsNum = statistic.getCommentsNum();
            //封装到Article
            artilce.setHits(hits);
            artilce.setCommentsNum(commentsNum);
        }
*/
        return recentArticles;
    }
    //发布新文章
    @Override
    public void publishArticle(Article article) {
        //添加到t_article表格
        //数据初始化
        article.setCreated(new Date());
        article.setCategories("默认分类");
        article.setTags("默认标签");
        article.setHits(0);
        article.setCommentsNum(0);
        articleMapper.addArticle(article);
        //添加到t_statistic表格
        Statistic statistic = new Statistic();
        statistic.setArticleId(article.getId());
        statistic.setHits(0);
        statistic.setCommentsNum(0);
        statisticMapper.addStatistic(statistic);
    }
    //修改文章
    @Override
    public void updateArticle(Article article) {
        //补充数据modify
        article.setModified(new Date());
        //1.修改文章数据
        articleMapper.updateArticle(article);
        //2.删除redis数据
        redisTemplate.delete("article_"+article.getId());
    }

    @Override
    public void deleteArticle(Integer id) {
        //删除文章
        articleMapper.deleteArticle(id);
        //删除评论
        commentMapper.deleteByArticleId(id);
        //删除统计数据
        statisticMapper.deleteByArticleId(id);
        //删除redis缓存
        redisTemplate.delete("article_"+id);
    }

}
